// 压型 编辑 创建 表单
<template>
  <div class="form-container card">
    <el-form
      class="form"
      ref="operaForm"
      :model="operaForm"
      label-width="100px"
    >
      <template >
        <el-form-item
          v-for="item in operaForm"
          :key="item.id"
          :prop="item.prop"
          :label="item.label"
        >
          <!-- 渲染常规表单数据 不是送电曲线 也不是时间选择器 只是普通的表单 -->
          <!-- <el-input
            v-if="item.prop !== 'granularity_size' && item.prop !== 'weight' && item.prop !== 'stipulate_time'"
            :placeholder="item.placeholder"
            v-model="operaInfo[item.prop]"
          /> -->
          <el-input
            v-if="item.prop !== 'granularity_size' && item.prop !== 'weight'"
            :placeholder="item.placeholder"
            v-model="operaInfo[item.prop]"
          />
          <el-input
            v-if="item.prop === 'granularity_size'"
            :placeholder="item.placeholder"
            v-model="operaInfo.granularity[item.what].granularity_size"
          />
          <el-input
            v-if="item.prop === 'weight'"
            :placeholder="item.placeholder"
            v-model="operaInfo.granularity[item.what].weight"
          />
          <!-- 时间选择器 -->
          <!-- <el-time-select
            v-if="item.prop === 'stipulate_time'"
            v-model="operaInfo[item.prop]"
            :picker-options="{
              start: '00:00',
              step: '00:15',
              end: '23:45',
            }"
            :placeholder="item.placeholder"
          >
          </el-time-select> -->
        </el-form-item>
      </template>
    </el-form>
    <div class="btn">
      <!-- 管理员不可编辑 -->
      <el-button type="primary" @click="changeInfo()"
        >提交</el-button
      >
      <el-button @click="returnPage">返回</el-button>
    </div>
  </div>
</template>

<script>
import {AddScreening, AddIngredients, UpdateScreening, UpdateIngredient} from '@/api/forming.js'
import { AddHanSuc, UpdateHanSuc } from '@/api/tunnelKiln.js'
export default {
  name: '',
  components: {},
  props: [],
  data () {
    return {
      // 哪个表单
      kind: '',
      // 哪个操作
      handle: '',
      operaForm: {},
      // 编辑传的数据
      operaInfo: {},
      ingredients: {
        // batch_number: '',
        // creator: '', // 创建者
        // describe: '', // 描述
        // createdAt: '', // 后端生成时间
        // desc: '', // 备注
        // out_boiler_time: '',
        // second_addoil: '', // float
        // first_addoil: '', // float
        // oil_time: '',
        // baiting_time: '',
        // pot_number: '',
        // operator: '',
        // class: '',
        // divisions: ''
      },
      screening: {
        creator: '',
        // createdAt: '',
        batch_number: '',
        norms: '',
        production_name: '',
        granularity: [
          {
            granularity_size: '16.0',
            weight: '' // int
          },
          {
            granularity_size: '12.0',
            weight: ''
          },
          {
            granularity_size: '10.0',
            weight: '' // int
          },
          {
            granularity_size: '8.0',
            weight: ''
          },
          {
            granularity_size: '6.0',
            weight: '' // int
          },
          {
            granularity_size: '4.0',
            weight: ''
          },
          {
            granularity_size: '2.0',
            weight: ''
          },
          {
            granularity_size: '1.0',
            weight: ''
          },
          {
            granularity_size: '0.5',
            weight: ''
          },
          {
            granularity_size: '0.15',
            weight: ''
          },
          {
            granularity_size: '0.075',
            weight: ''
          },
          {
            granularity_size: '-0.075',
            weight: ''
          }
        ],
        loss: '', // float
        desc: '',
        describe: '',
        // 无用数据
        handover_person: '0', // 交班人
        stipulate_time: '', // 前端时间
        successor: '0'
      }
    }
  },
  computed: {},
  watch: {},
  created () {
    const { handle, kind } = this.$route.query
    this.kind = kind
    this.handle = handle
    if (handle === '编辑') {
      this.operaInfo = this.$route.query.info
      // console.log('刚开始screening', this.operaInfo)
    } else if (handle === '创建' && kind === 'ingredients') {
      // console.log('刚开始ingredients', this.operaInfo)
      this.operaInfo = this.ingredients
    } else if (handle === '创建' && kind === 'screening') {
      this.operaInfo = this.screening
      // console.log('刚开始screening', this.operaInfo)
    }
    this.getOperaForm()
  },
  mounted () {},
  methods: {
    changeInfo () {
      if (this.handle === '创建') {
        switch (this.kind) {
          case 'screening':
            for (let i = 0; i < this.operaForm.length; i++) {
              if (this.operaForm[i].type === 'int') {
                if (this.operaForm[i].what !== undefined) {
                  this.operaInfo.granularity[this.operaForm[i].what].weight = parseInt(this.operaInfo.granularity[this.operaForm[i].what].weight)
                } else {
                  this.operaInfo[this.operaForm[i].prop] = parseInt(this.operaInfo[this.operaForm[i].prop])
                }
              } else if (this.operaForm[i].type === 'float') {
                if (this.operaForm[i].what !== undefined) {
                  this.operaInfo.granularity[this.operaForm[i].what].weight = parseFloat(this.operaInfo.granularity[this.operaForm[i].what].weight)
                } else {
                  this.operaInfo[this.operaForm[i].prop] = parseFloat(this.operaInfo[this.operaForm[i].prop])
                }
              }
            }
            // console.log('this.operaInfo', this.operaInfo)
            AddScreening(this.operaInfo).then(res => {
              console.log(this.operaInfo, '发给后端的数据')
              console.log('AddScreening', res)
              if (res.data.code === 1000) {
                this.$message.success('创建成功')
              }
            }).catch(err => {
              console.log(err)
              // this.$message.info('创建失败')
            })
            break
          case 'ingredients':
            // console.log(this.ingredients, 'this')
            // console.log(this.operaInfo, 'operInfo')
            for (let i = 0; i < this.operaForm.length; i++) {
              if (this.operaForm[i].type === 'int') {
                this.operaInfo[this.operaForm[i].prop] = parseInt(this.operaInfo[this.operaForm[i].prop])
              } else if (this.operaForm[i].type === 'float') {
                this.operaInfo[this.operaForm[i].prop] = parseFloat(this.operaInfo[this.operaForm[i].prop])
              }
            }
            AddIngredients(this.operaInfo).then(res => {
              console.log(this.operaInfo, '发给后端的数据')
              // console.log(res, '创建配料表')
              if (res.data.code === 1000) {
                this.$message.success('创建成功')
              }
            }).catch(err => {
              console.log(err)
              // this.$message.info('创建失败')
            })
            break
          case 'handover':
            for (let i = 0; i < this.operaForm.length; i++) {
              if (this.operaForm[i].type === 'int') {
                this.operaInfo[this.operaForm[i].prop] = parseInt(this.operaInfo[this.operaForm[i].prop])
              } else if (this.operaForm[i].type === 'float') {
                this.operaInfo[this.operaForm[i].prop] = parseFloat(this.operaInfo[this.operaForm[i].prop])
              }
            }
            AddHanSuc(this.operaInfo).then(res => {
              console.log(this.operaInfo, '发给后端的数据')
              // console.log(res, '创建配料表')
              if (res.data.code === 1000) {
                this.$message.success('创建成功')
              }
            }).catch(err => {
              console.log(err)
            })
            break
          default:
            console.log('出错了')
            break
        }
        sessionStorage.setItem('formingActiveName', this.kind)
        this.$router.go(-1)
      } else if (this.handle === '编辑') {
        switch (this.kind) {
          case 'screening':
            for (let i = 0; i < this.operaForm.length; i++) {
              if (this.operaForm[i].type === 'int') {
                if (this.operaForm[i].what !== undefined) {
                  this.operaInfo.granularity[this.operaForm[i].what].weight = parseInt(this.operaInfo.granularity[this.operaForm[i].what].weight)
                } else {
                  this.operaInfo[this.operaForm[i].prop] = parseInt(this.operaInfo[this.operaForm[i].prop])
                }
              } else if (this.operaForm[i].type === 'float') {
                if (this.operaForm[i].what !== undefined) {
                  this.operaInfo.granularity[this.operaForm[i].what].weight = parseFloat(this.operaInfo.granularity[this.operaForm[i].what].weight)
                } else {
                  this.operaInfo[this.operaForm[i].prop] = parseFloat(this.operaInfo[this.operaForm[i].prop])
                }
              }
            }
            UpdateScreening(this.operaInfo).then(res => {
              // console.log('更新筛分', this.operaInfo)
              // console.log('更新筛分表', res)
              if (res.data.code === 1000) {
                this.$message.success('编辑成功')
              }
            })
            break
          case 'ingredients':
            UpdateIngredient(this.operaInfo).then(res => {
              // console.log('更新配料表', res)
              if (res.data.code === 1000) {
                this.data.$message.success('编辑成功')
              }
            })
            break
          case 'handover':
            UpdateHanSuc(this.operaInfo).then(res => {
              // console.log('更新配料表', res)
              if (res.data.code === 1000) {
                this.data.$message.success('编辑成功')
              }
            })
            break
        }
        sessionStorage.setItem('formingActiveName', this.kind)
        this.$router.go(-1)
      }
    },
    returnPage () {
      sessionStorage.setItem('formingActiveName', this.kind)
      this.$router.go(-1)
    },
    async getOperaForm () {
      const res = await this.$axios.get(`http://127.0.0.1:7002/${this.kind}form`)
      this.operaForm = res.data.tableData
      // console.log('operaForm', this.operaForm)
    }
  }
}
</script>

<style lang="scss" scoped>
.form {
  padding: 40px 0;
}
.btn {
  text-align: right;
}
.el-form-item {
  /* width: 100px; */
  display: inline-block !important;
}
</style>
